package simbot.example.util;


import com.alibaba.fastjson.JSONObject;
import simbot.example.entity.ItemsJiu;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.List;

/**
 * @Desecription: 通用工具
 * @Autor: wxl
 * @Date: 2020/7/10 16:31
 */
public class CommonUtil {


    private static BufferedImage image = null;
    private static Graphics mainGraphics = null, graphics = null;
    private static Container panel = null;
    private static ByteArrayOutputStream outputStream = null;


    public static List<JSONObject> jsonToList(String str) {
        List<JSONObject> jsonList = new ArrayList<>();
        if (JSONObject.isValidArray(str)) {
            jsonList = JSONObject.parseArray(str, JSONObject.class);
        } else {
            JSONObject row = JSONObject.parseObject(str);
            if (row != null) {
                jsonList.add(row);
            }
        }
        return jsonList;
    }

    public static List<String> jsonToStringList(JSONObject json) {
        String str = json.toJSONString();
        List<String> jsonList = new ArrayList<>();
        if (JSONObject.isValidArray(str)) {
            jsonList = JSONObject.parseArray(str, String.class);
        } else {
            String row = str.replace("[", "").replace("]", "");
            if (row != null) {
                jsonList.add(row);
            }
        }
        return jsonList;
    }

    public static double divide(double a, double b, int scale) {
        BigDecimal bd1 = new BigDecimal(Double.toString(a));
        BigDecimal bd2 = new BigDecimal(Double.toString(b));
        return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    //链接url下载图片
    public static void downloadPicture(String urlList, String path) {
        URL url = null;
        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            BASE64Encoder encoder = new BASE64Encoder();
            String encode = encoder.encode(buffer);//返回Base64编码过的字节数组字符串
            System.out.println(encode);
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public synchronized static String getNo() throws InterruptedException {
        String cardNo = String.valueOf(System.currentTimeMillis());
        return cardNo.substring(0, 11);
    }

    public static void getImage(String name, Integer belief, Integer money, String record, List<ItemsJiu> list){
        File file=new File("D:\\steam\\steamapps\\common\\图库\\早苗.jpg");
        try {
            image= ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        graphics=image.getGraphics();//得到图片的画笔
        graphics.setFont(new Font("黑体",0,40));//设置画笔的样式
        graphics.setColor(Color.black);//设置画笔的颜色
        graphics.drawString("昵称："+name,200,200);
        graphics.drawString("信仰："+belief,200,250);
        graphics.drawString("金币："+money,200,300);
        graphics.drawString("事件记录："+record,200,500);
        graphics.drawString("背包",200,600);


        Graphics2D g = image.createGraphics();
        int cont = 0;
        for (ItemsJiu item:list){
            if (item.getItemname().equals("咖啡")){
                try {
                    g.drawImage(ImageIO.read(new File("D:\\steam\\steamapps\\common\\shop\\咖啡.png")),200+93*cont ,650 , 83, 86, null);
                    graphics.setFont(new Font("黑体",0,15));
                    graphics.setColor(Color.white);
                    graphics.drawString(item.getQuantity().toString(),273+93*cont,726);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                cont++;
            }
            if (item.getItemname().equals("奶茶")){
                try {
                    g.drawImage(ImageIO.read(new File("D:\\steam\\steamapps\\common\\shop\\奶茶.png")),200+93*cont ,650 , 83, 86, null);
                    graphics.setFont(new Font("黑体",0,15));
                    graphics.setColor(Color.white);
                    graphics.drawString(item.getQuantity().toString(),273+93*cont,726);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                cont++;
            }
            if (item.getItemname().equals("蛋糕")){
                try {
                    g.drawImage(ImageIO.read(new File("D:\\steam\\steamapps\\common\\shop\\蛋糕.png")),200+93*cont ,650 , 83, 86, null);
                    graphics.setFont(new Font("黑体",0,15));
                    graphics.setColor(Color.white);
                    graphics.drawString(item.getQuantity().toString(),273+93*cont,726);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                cont++;
            }
        }
        g.dispose();

        outputStream=new ByteArrayOutputStream();
        try {
            ImageIO.write(image,"JPG",outputStream);//将字节流进行后缀的修饰，并将图片放入字节流中
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        byte[] content=outputStream.toByteArray();//得到字节流中的所有字节
        try(BufferedOutputStream inputStream=new BufferedOutputStream(new FileOutputStream("D:\\steam\\steamapps\\common\\图库\\2.jpg"))){
            //将所传过来的字节流中的字节设置为输出流
            inputStream.write(content);//将字节写出到指定位置
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
